# Austerity and Aggression: Government Responses to IMF Conditionality
# Manuscript Figure 3 Replication Code
# World Politics

# Figure 3: Frequency of Military Action ####

# Load packages
library(tidyr)
library(dplyr)
library(countrycode)
library(ggplot2)

##->## Empty country-year dataframe
states <- read.csv("states2016.csv", fileEncoding="UTF-8-BOM") # load CoW state list downloaded 9/21/21
states %>%
  # This mutate command below is optional.
  # Prepare the pipe to think rowwise. If you don't, the next mutate command will fail.
  rowwise() %>%
  # Create a list in a tibble that we're going to expand soon.
  mutate(year = list(seq(1978, 2016))) %>%
  # Unnest the list, which will expand the data.
  unnest(cols = c(year)) %>%
  # Arrange by ccode, year, just to be sure.
  arrange(ccode, year) %>%
  # Select just the ccode, year
  select(ccode, statenme, year) %>%
  # Make sure there are no duplicate country-year observations.
  # There shouldnt' be. But be sure.
  # Finally: assign to object.
  distinct(ccode, statenme, year) -> CY
rm(states)

##->## Merge in MIDs
mids <- read.csv("MIDB 5.0.csv", fileEncoding="UTF-8-BOM") # Load MIDs incident-level dataset downloaded 9/21/21
mids <- mids[mids$orig == 1,] # subset to cases where state initiates
mids$mid <- 1
colnames(mids)[6] <- "year"

# create var to count number of MIDs per initiator
mids <- mids %>%
  group_by(ccode,year) %>%
  summarise(num_mids = sum(mid), hiact = unique(hiact)) %>%
  ungroup()
table(mids$num_mids)

CY <- merge(unique(CY), unique(mids), by = c("ccode", "year"), all.x=T) # merge wars into shell
CY$ccode <- countrycode(CY$ccode, origin = "cown", destination = "iso3c") # change ccode format
CY <- CY[!is.na(CY$ccode),] # delete NA ccodes
CY$mids[is.na(CY$num_mids)] <- 0 # change NA war to 0

# Main IMF dataset, no lags
prelim <- read.csv("prelim.csv")
# subset to only include relevant vars for plot (country, year, and IMF count)
prelim <- prelim[,c("ccode", "year", "count")]

# Merge CY with prelim to identify number of MIDs initiated by IMF loan recipients
plot <- left_join(unique(CY), unique(prelim), by = c("ccode", "year"), 
                  relationship = "many-to-many")

# remove all obs where count (the main IV/count of IMF conditions) == NA
plot <- subset(plot, !is.na(plot$count))
table(plot$hiact)

# recode hiact variable with descriptive labels (from MID dataset codebook)
plot <- plot %>%
  mutate(hiact = recode(hiact, `0` = "No militarized action", 
                        `1` = "Threat to use force", 
                        `4` = "Threat to declare war",
                        `7` = "Show of force", 
                        `8` = "Alert",
                        `10` = "Mobilization",
                        `11` = "Fortify border", 
                        `12` = "Border violation",
                        `13` = "Blockade", 
                        `14` = "Occupation of territory", 
                        `15` = "Seizure", 
                        `16` = "Attack", 
                        `17` = "Clash" , 
                        `20` = "Begin interstate war",
                        `21` = "Join interstate war"))

plot %>% 
  filter(!is.na(hiact)) %>%
  filter(year < 2015) %>%
  group_by(hiact) %>% 
  summarise(count = n()) %>% 
  ggplot(aes(y = reorder(hiact,(count)), x = count)) + 
  geom_bar(stat = "identity", color = "steelblue4", fill = "steelblue4") + 
  theme_bw() + 
  xlab("") + 
  ylab("") + 
  theme(text = element_text(size=20))
